package hui.com.controller.sales;

import hui.com.authority.Authority;
import hui.com.authority.UsersLogEntity;
import hui.com.model.entity.sales.SalesEntity;
import hui.com.model.req.sales.RequestGetLikeSales;
import hui.com.model.res.MyResponse;
import hui.com.service.SalesService;
import hui.com.util.RedisUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@CrossOrigin(origins = "*")//处理跨源资源共享
@RequestMapping("/public/sales")
@Controller
public class sales {
    @Resource
    SalesService salesService;
    /**
     * 新增：前置条件，需要有指定编号。接口为salesADD_getSalesCode
     * @param sales sales
     * @return boolean
     */
    @PostMapping("/salesADD")
    @ResponseBody
    @Authority("sales_add")
    public Object salesADD(@RequestBody SalesEntity sales){
        if (sales==null){
            return MyResponse.builder().code(202).msg("传输为空");
        }
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

        UsersLogEntity usersLog = RedisUtil.getValue(request.getHeader("Authorization"));

        sales.setUID(usersLog.getUID());
        sales.setUpdUID(usersLog.getUID());
        sales.setSalesCode(salesService.salesADD_getCode());
        return MyResponse.builder().data(salesService.salesADD(sales)).code(200).build();

    }
    @PostMapping("/salesADD_getCode")
    @ResponseBody
    @Authority("sales_add")
    public Object salesADD_getCode() {
        return MyResponse.builder().data(salesService.salesADD_getCode()).code(200).build();

    }

    /**
     * 获取 可以绑定的预测销售表单
     * @return
     */
    @PostMapping("/salesADD_getSalesForecastList")
    @ResponseBody
    @Authority("sales_add")
    public Object salesADD_getSalesForecastList(@RequestBody(required = false)String query) {
        RequestGetLikeSales sales = new RequestGetLikeSales();
        sales.setSalesALL(query);
        return MyResponse.builder().data(salesService.salesADD_getSalesForecastList(sales)).code(200).build();

    }

    /**
     * 查询销售表集
     * @return
     */
    @PostMapping("/getSalesList")
    @ResponseBody
    @Authority("sales_show")
    public Object getSalesList(@RequestBody(required = false) RequestGetLikeSales sales) {
        if(sales==null){
            return MyResponse.builder().msg("没有对象参数").code(202).build();

        }
        return MyResponse.builder().data(salesService.getListSales(sales)).code(200).build();

    }

    /**
     * 查询指定销售表，通过code
     */

    @PostMapping("/getSales")
    @ResponseBody
    @Authority("sales_show")
    public Object getSalesList(@RequestBody(required = false) SalesEntity sales) {
        if(sales==null){
            return MyResponse.builder().msg("没有对象参数").code(202).build();

        }
        return MyResponse.builder().data(salesService.getSales(sales)).code(200).build();

    }


    /**
     * 查询指定销售表，通过code
     */

    @PostMapping("/updSales")
    @ResponseBody
    @Authority("sales_upd")
    public Object updSales(@RequestBody(required = false) SalesEntity sales) {

        if(sales==null){
            return MyResponse.builder().msg("没有对象参数").code(202).build();

        }

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();

        UsersLogEntity usersLog = RedisUtil.getValue(request.getHeader("Authorization"));
        // 历史记录存储
        salesService.salesHistoryADD(salesService.getSales(sales));

        sales.setUID(usersLog.getUID());
        sales.setUpdUID(usersLog.getUID());
        // 正常修改
        Boolean a = salesService.updateSales(sales);

        return MyResponse.builder().data(a).code(200).build();

    }

    /**
     * 查询指定历史记录，通过code
     */

    @PostMapping("/getSalesHistoryList")
    @ResponseBody
    @Authority("sales_show")
    public Object getSalesHistoryList(@RequestBody(required = false) SalesEntity sales) {
        if(sales==null){
            return MyResponse.builder().msg("没有对象参数").code(202).build();

        }
        return MyResponse.builder().data(salesService.getSalesHistoryList(sales)).code(200).build();

    }
}
